using Engine;
using Engine.Graphics;
using Game;

namespace StyleSurvivalcraft;

public class StyleWeapons
{
    public ComponentPlayer componentPlayer;
    public abstract class AnimatedBlock : Block
    {
        public abstract int BlockIndex { get; } 
        public abstract Texture2D[] Textures { get; } 
        public abstract float FrameDuration { get; } 
        
        protected BlockMesh m_standaloneBlockMesh = new BlockMesh();
        private int currentFrame;
        private float frameTimer;
        private int lastFrameTickCount;

        public override void Initialize()
        {
            Model model = ContentManager.Get<Model>("Models/FlatAnimatedBlock");
            Matrix boneAbsoluteTransform = BlockMesh.GetBoneAbsoluteTransform(model.FindMesh("Plate").ParentBone);
            m_standaloneBlockMesh.AppendModelMeshPart(
                model.FindMesh("Plate").MeshParts[0],
                boneAbsoluteTransform * Matrix.CreateTranslation(0f, -0.1f, 0f) * Matrix.CreateRotationX(1.5707f),
                false, false, true, false, Color.White);

            lastFrameTickCount = Environment.TickCount;
            base.Initialize();
        }

        public override void GenerateTerrainVertices(BlockGeometryGenerator generator, TerrainGeometry geometry, 
            int value, int x, int y, int z)
        {
            generator.GenerateShadedMeshVertices(this, x, y, z, m_standaloneBlockMesh, 
                Color.White, null, null, geometry.SubsetOpaque);
        }

        public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, 
            float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
        {
            UpdateAnimation();
            DrawCurrentFrame(primitivesRenderer, color, size, ref matrix, environmentData);
        }

        private void UpdateAnimation()
        {
            int currentTick = Environment.TickCount;
            float deltaTime = (currentTick - lastFrameTickCount) / 1000f;
            lastFrameTickCount = currentTick;

            frameTimer += deltaTime;
            if (frameTimer >= FrameDuration)
            {
                frameTimer -= FrameDuration;
                currentFrame = (currentFrame + 1) % Textures.Length;
            }
        }

        private void DrawCurrentFrame(PrimitivesRenderer3D renderer, Color color, float size, 
            ref Matrix matrix, DrawBlockEnvironmentData environmentData)
        {
            if (Textures.Length == 0) return;
            
            BlocksManager.DrawMeshBlock(renderer, m_standaloneBlockMesh, 
                Textures[currentFrame], color, 4f * size, 
                ref matrix, environmentData);
        }
    }

    public class Ice_Sword : AnimatedBlock
    {
        public static int Index => 1; 

        public override int BlockIndex => Index;
        public override float FrameDuration => 0.2f;

        public override Texture2D[] Textures => new Texture2D[]
        {
            ContentManager.Get<Texture2D>("Textures/Weapons/ice (1)"),
            ContentManager.Get<Texture2D>("Textures/Weapons/ice (2)"),
            ContentManager.Get<Texture2D>("Textures/Weapons/ice (3)"),
            ContentManager.Get<Texture2D>("Textures/Weapons/ice (4)")
        };

        public Ice_Sword()
        {
            DefaultDisplayName = "冰剑";
            DefaultDescription = "冰冷的剑，可以用来杀戮敌人。";
            DefaultCategory = "StyleSurvivalcraft";
            InHandScale = 0.2f;
            FirstPersonScale = 0.4f;
            FirstPersonOffset = new Vector3(1f, -0.5f, -1f);
            IsPlaceable = false;
            MaxStacking = 1;
            DefaultMeleePower = 9;
            DefaultMeleeHitProbability = 0.9f;
            ToolLevel = 4;
            FirstPersonRotation = new Vector3(10f, -10f, 40f);
        }
    }
    public class Fire_Sword : AnimatedBlock
    {
        public static int Index => 2; 

        public override int BlockIndex => Index;
        public override float FrameDuration => 0.2f;

        public override Texture2D[] Textures => new Texture2D[]
        {
            ContentManager.Get<Texture2D>("Textures/Weapons/fire (1)"),
            ContentManager.Get<Texture2D>("Textures/Weapons/fire (2)"),
            ContentManager.Get<Texture2D>("Textures/Weapons/fire (3)"),
            ContentManager.Get<Texture2D>("Textures/Weapons/fire (4)")
        };
        public Fire_Sword()
        {
            DefaultDisplayName = "火剑";
            DefaultDescription = "火热的剑，可以用来烤肉。";
            DefaultCategory = "StyleSurvivalcraft";
            InHandScale = 0.2f;
            FirstPersonScale = 0.4f;
            FirstPersonOffset = new Vector3(1f, -0.5f, -1f);
            IsPlaceable = false;
            MaxStacking = 1;
            DefaultMeleePower = 9;
            DefaultMeleeHitProbability = 0.9f;
            ToolLevel = 4;
            FirstPersonRotation = new Vector3(10f, -10f, 40f);
        }
    }

    public class Entity_Copy_Stone : AnimatedBlock
    {
        public static int Index => 3; 

        public override int BlockIndex => Index;
        public override float FrameDuration => 0.2f;

        public override Texture2D[] Textures => new Texture2D[]
        {
            ContentManager.Get<Texture2D>("Textures/Others/Entity_Copy_Stone"),
        };

        public Entity_Copy_Stone()
        {
            DefaultDisplayName = "生物复制石";
            DefaultDescription = "可以复制生物。";
            DefaultCategory = "StyleSurvivalcraft";
            InHandScale = 0.2f;
            FirstPersonScale = 0.4f;
            FirstPersonOffset = new Vector3(1f, -0.5f, -1f);
            IsPlaceable = false;
            MaxStacking = 1;
            DefaultMeleePower = 9;
            DefaultMeleeHitProbability = 0.9f;
        }
    }
}